<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin;
use App\Role;
use Hash;
class adminController extends Controller
{
    static $tableName='admins';
    /**
    * 软删除Trait
    */
    use \App\Http\utils\trashedMethods;
    /**
     * 登陆页面
     */
    public function loginIndex(Request $request){
        return view('admin.admin.login');
    }
    /**
     * 登录处理
     */
    public function login(Request $request){
        $req=$request->input();
        //参数验证
        if(empty($req['account'])||empty($req['password'])){
            return json(101,'请填写用户名密码');
        }
        //查询用户
        $admin=Admin::where('account',$req['account'])->first();
        if(!$admin){
            return json(102,'用户不存在');
        }
        //验证密码
        $isInvalid=Hash::check($req['password'],$admin->password);
        if($isInvalid){
            //保存用户相关信息到session 与菜单
            $role=Role::select('menu_ids')->find($admin->role_id);
            if(!$role){
                return json(103,'当前用户未分配权限');
            }
            //保存相关信息到SESSION
            $request->session()->put([
                'user_token'=>$admin->token,
                'user_id'=>$admin->id,
                'avatar'=>$admin->avatar,
                'nickname'=>$admin->nickname,
                'menu_ids'=>$role->menu_ids,
            ]);
            //登录次数自增
            $admin->increment('login_count');
            //登录IP记录
            $admin->prev_ip=$request->ip();
            $admin->save();
            return json(200,'登录成功');
        }else{
            return json(103,'密码错误');
        }
    }
    
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $keyWord=$request->input('keyWord');
        $is_trashed=$request->input('trashed');
        if($is_trashed){
            $pageListData=Admin::where("account","like","%".$keyWord."%")->onlyTrashed()->orderbyDesc('sort')->orderByDesc('updated_at')->paginate(8);
        }else{
            $pageListData=Admin::where("account","like","%".$keyWord."%")->orderbyDesc('sort')->orderByDesc('updated_at')->paginate(8);
        }
        return view('admin.admin.index',compact('pageListData','is_trashed','keyWord'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $roleList=Role::orderBy('sort','desc')->get();
        return view('admin.admin.form',compact('roleList'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $schema=[
            'account'=>'required',
            'password'=>'required',
            'role_id'=>'required'
        ];
        $request->validate($schema);

        //密码加密
        $createData=$request->only(['account','password','avatar','role_id','sort']);

        //用户名是否重复
        if(Admin::where('account',$createData['account'])->exists()){
            return json(101,'用户名重复');
        }
        $hashPassword=Hash::make($request->password);
        $createData['password']=$hashPassword;
        //生成token
        $hashToken=Hash::make(time().mt_rand());
        $createData['token']=$hashToken;
        //创建
        Admin::create($createData);
        return json(200,'创建成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $roleList=Role::orderBy('sort','desc')->get();
        $editData=Admin::find($id);
        return view('admin.admin.form',compact('editData','roleList'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $schema=[
            'role_id'=>'required'
        ];
        $request->validate($schema);
        $updateData=$request->only(['nickname','avatar','role_id','sort']);

        //修改用户信息 更新当前用户TOKEN
        $hashToken=Hash::make(time().mt_rand());
        $updateData['token']=$hashToken;
        
        Admin::find($id)->update($updateData);
        return json(200,'修改成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res=Admin::find($id)->delete();
        if($res) return json(200,'删除成功');
        return json(105,'删除失败');
    }
}
